package com.masterclient.app.businessmodule.homemodule.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.adapter.EcItemAdapter;
import com.masterclient.app.businessmodule.homemodule.contract.EcItemContract;
import com.masterclient.app.businessmodule.homemodule.contract.EcItemPresenter;
import com.masterclient.app.modules.income.model.EcItemDataBean;
import com.masterclient.app.modules.income.model.MasterMienDataBean;
import com.masterclient.app.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/8/4 13:17
 * 功能说明：
 */
public class ECItemFragment extends BaseFragment<EcItemPresenter> implements EcItemContract.View {
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.view_no_data)
    View viewNoData;

    List<EcItemDataBean.ProductsItem> list;
    private boolean hasMore;
    private int page = 1;
    private String categoryId;

    private EcItemAdapter recycleAdapter;
    private OnSuccessCallBack onSuccessCallBack;

    interface OnSuccessCallBack{
        void onLoadSuccess(EcItemDataBean.HotProductsItem hotProductsItem);
    }

    public void setOnSuccessCallBackListener(OnSuccessCallBack onSuccessCallBack){
        this.onSuccessCallBack = onSuccessCallBack;
    }

    public static ECItemFragment getInstance(String categoryId) {
        ECItemFragment fragment = new ECItemFragment();
        Bundle bundle = new Bundle();
        bundle.putString("categoryId",categoryId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_ec_item, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        categoryId = getArguments().getString("categoryId");
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recycleAdapter = new EcItemAdapter();
        recycleAdapter.setOnItemClickListener((adapter, view, position) -> {
            EcItemDataBean.ProductsItem item = list.get(position);
            CommonUtil.goAnyWhere(mContext,item.productUrl,"","","","","");
        });
        recyclerView.setAdapter(recycleAdapter);
        recycleAdapter.bindToRecyclerView(recyclerView);

        recycleAdapter.setOnLoadMoreListener(() -> {
            if (hasMore) {
                mPresenter.getProducts(String.valueOf(page + 1), PAGE_SIZE,categoryId);
            }
        }, recyclerView);

        swipeLayout.setOnRefreshListener(() -> {
            if (list != null) {
                list.clear();
                recycleAdapter.notifyDataSetChanged();
            }
            recycleAdapter.setEnableLoadMore(true);
            page = 1;
            mPresenter.getProducts(String.valueOf(page), PAGE_SIZE,categoryId);
        });
        mPresenter.getProducts(String.valueOf(page), PAGE_SIZE,categoryId);
    }

    @Override
    public void getProductsSuccess(EcItemDataBean dataBean) {
        if (swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }

        if (dataBean.hotProduct != null) {
            onSuccessCallBack.onLoadSuccess(dataBean.hotProduct);
        }

        this.page = Integer.parseInt(dataBean.page);
        if (list == null) {
            list = new ArrayList<>();
        }
        if (page == 1) {
            list.clear();
            page++;
            mPresenter.getProducts(String.valueOf(page), PAGE_SIZE,categoryId);
        }

        if (page == 1 && (dataBean.products == null || dataBean.products.size() == 0)) {
            swipeLayout.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);
            viewNoData.setVisibility(View.VISIBLE);
        } else {
            if(page == 2 && list.size() == 0 && (dataBean.products == null || dataBean.products.size() == 0)){
                swipeLayout.setVisibility(View.GONE);
                recyclerView.setVisibility(View.GONE);
                viewNoData.setVisibility(View.VISIBLE);
            }else {
                swipeLayout.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.VISIBLE);
                viewNoData.setVisibility(View.GONE);
                list.addAll(dataBean.products);
                recycleAdapter.setNewData(list);
                recycleAdapter.setEnableLoadMore(true);
                hasMore = true;
                if (page > 1 && (dataBean.products == null || dataBean.products.size() == 0)) {
                    hasMore = false;
                    recycleAdapter.setEnableLoadMore(false);
                }
                recycleAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void getProductsFailed(String msg) {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
        }
        showToast(msg);
    }
}
